package com.study.cloud.apilog.sls;

import com.aliyun.openservices.aliyun.log.producer.Callback;
import com.aliyun.openservices.aliyun.log.producer.Result;
import com.aliyun.openservices.log.common.LogItem;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.atomic.AtomicLong;

/**
 * @Date 2022/5/17
 * @Author Cliff
 * desc
 */
public class SampleCallback implements Callback {

    private static final Logger LOGGER = LoggerFactory.getLogger(SampleCallback.class);

    private final String project;

    private final String logStore;

    private final LogItem logItem;

    private final AtomicLong completed;

    public SampleCallback(String project, String logStore, LogItem logItem, AtomicLong completed) {
        this.project = project;
        this.logStore = logStore;
        this.logItem = logItem;
        this.completed = completed;
    }

    @Override
    public void onCompletion(Result result) {
        try {
            if (result.isSuccessful()) {
                LOGGER.info("发送成功.");
            } else {
                LOGGER.error(
                        "日志发送失败, project={}, logStore={}, logItem={}, result={}",
                        project,
                        logStore,
                        logItem.ToJsonString(),
                        result);
            }
        } finally {
            completed.getAndIncrement();
        }
    }
}
